package com.itdianzi.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itdianzi.dao.cargo.FactoryDao;
import com.itdianzi.dao.cargo.ProductDao;
import com.itdianzi.domain.cargo.Product;
import com.itdianzi.domain.cargo.ProductExample;
import com.itdianzi.service.cargo.ProductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    private FactoryDao factoryDao;

    /**
     * 查询所有产品信息
     * @param page
     * @param size
     * @param productExample
     * @return
     */
    @Override
    public PageInfo selectByExample(Integer page, Integer size, ProductExample productExample) {
        PageHelper.startPage(page, size);
        List<Product> productList = productDao.selectByExample(productExample);
        return new PageInfo(productList);
    }

    /**
     * 根据ID查询出对应的产品信息
     * @param id
     * @return
     */
    @Override
    public Product findById(String id) {
        return productDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Product product) {
        //设置产品ID
        product.setId(UUID.randomUUID().toString());
        //保存到数据库
        productDao.insertSelective(product);
    }

    @Override
    public void update(Product product) {
        productDao.updateByPrimaryKeySelective(product);
    }

    @Override
    public void deleteById(String id) {
        productDao.deleteByPrimaryKey(id);
    }
}
